// pages/purchaserecord/purchaserecord.js
const app = getApp();
const httpUtils = require('../../utils/httpUtils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    maskflag: true,
    loginflag: true,
    orderlist: [],
    total: null,
  
    showSearchResult: false,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {


  },
  getData: function () {
   
    let that = this
    that.setData({
      page: this.data.page
    });
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.orderlist({
      access_token: app.globalData.$TOKEN,
      page: that.data.page,
      size: 10,
    }).then((res) => {
      wx.hideLoading()

      if (that.data.page == 1) {
        app.globalData.userDetail.doubleminus_type=res.data.doubleminus_type
        that.data.orderlist.push(...res.data.list);
      } else {
        that.data.orderlist.push(...res.data.list);
      }
      that.data.page = that.data.page + 1;
      that.setData({
        orderlist: that.data.orderlist,
        page: that.data.page
      });
      console.log("that.data.orderlist.length:" +  that.data.orderlist.length);
      if (that.data.orderlist.length >= res.data.total) {
        that.setData({
          showSearchResult: false
        })
      } else {
        console.log("showSearchResult:" + that.data.showSearchResult);
        that.setData({
          showSearchResult: true
        })
      }

    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow")
    if (app.globalData.$TOKEN) {
      this.setData({
        page:1,
        orderlist: [],
        showSearchResult: false,
        total: null,
      })
      this.getData()
    } else {
      this.setData({
        userflag: true,
        maskflag: false,
        loginflag: false
      })

    }
  },
  bindPhone: function (e) {
    var phone = e.currentTarget.dataset['id'];
    var order_no = e.currentTarget.dataset['key'];
    wx.navigateTo({
      url: '/pages/bindUserInfo/bindUserInfo?phone='+phone+"&order_no="+order_no,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.showSearchResult==true) {
      console.log("11onReachBottom"+this.data.showSearchResult+this.data.page)
      this.getData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})